﻿using System;
using DistributedVisionRunner.Interface;
using DistributedVisionRunner.Interface.Model;
using DistributedVisionRunner.Module.Models;
using Prism.Commands;
using Prism.Services.Dialogs;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;

namespace DistributedVisionRunner.Module.ViewModels
{
    public class BoundarySettingDialogViewModel : DialogViewModelBase
    {
        private bool _login;
        private string _errorMessage;
        private Func<bool> _saveValidator;

        public bool Login
        {
            get => _login;
            set => SetProperty(ref _login, value);
        }

        public ObservableCollection<BoundaryCollectionViewModel> BoundaryCollectionViewModels { get; } =
            new ObservableCollection<BoundaryCollectionViewModel>();
        public ICommand OKCommand { get; }

        public string ErrorMessage
        {
            get => _errorMessage;
            set => SetProperty(ref _errorMessage, value);
        }

        public BoundarySettingDialogViewModel()
        {
            OKCommand = new DelegateCommand(() =>
            {

                // Range validation for all enabled boundaries
                var allBoundaries = BoundaryCollectionViewModels.Select(c => c.BoundaryItems).ToArray();
                foreach (var boundaryItemList in allBoundaries)
                {
                    var firstFailedBoundary = boundaryItemList?.FirstOrDefault(b => b.Enable && b.Ceiling <= b.Floor);
                    if (firstFailedBoundary != null)
                    {
                        ErrorMessage = $"{firstFailedBoundary.Name}的上下限设置有误";
                        return;
                    }
                }

                if (_saveValidator != null)
                {
                    var validated = _saveValidator();
                    if (!validated) return;
                }

                foreach (var collectionViewModel in BoundaryCollectionViewModels)
                {
                    collectionViewModel.Save();
                }

                RaiseRequestClose(new DialogResult(ButtonResult.OK));
            }, () => Login).ObservesProperty(() => Login);
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            base.OnDialogOpened(parameters);

            Login = parameters.GetValue<bool>("login");
            var unitName = parameters.GetValue<string>("unitName");
            var dialogService = parameters.GetValue<IDialogService>("dialogService");
            _saveValidator = parameters.GetValue<Func<bool>>("SaveValidator");


            void TryAddBoundaryCollection(List<BoundaryItem> boundaryItems, OutputDataType dataType)
            {
                if (boundaryItems?.Any() == true)
                {
                    BoundaryCollectionViewModels.Add(new BoundaryCollectionViewModel(dataType, boundaryItems,
                        unitName, dialogService));

                }
            }

            var rawBoundaries = parameters.GetValue<List<BoundaryItem>>("rawBoundaries");
            TryAddBoundaryCollection(rawBoundaries, OutputDataType.Raw);

            var weightedBoundaries = parameters.GetValue<List<BoundaryItem>>("weightedBoundaries");
            TryAddBoundaryCollection(weightedBoundaries, OutputDataType.Weighted);

            var reservedBoundaries = parameters.GetValue<List<BoundaryItem>>("reservedBoundaries");
            TryAddBoundaryCollection(reservedBoundaries, OutputDataType.Reserved);


        }



    }
}
